package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.service.handler.GuliException;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-08-23
 */
@RestController
@Api(description="讲师管理")
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("findAll")
    @ApiOperation(value = "所有讲师列表")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);

    }
    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "根据ID删除讲师")
    public R deleteLogic(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        boolean b = eduTeacherService.removeById(id);
        if(b==true){
            return R.ok();
        }else{
            return R.error();
        }
    }
    /**@ApiOperation(value = "分页讲师列表")
    @GetMapping("{current}/{limit}")
    public R pageTeacher(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit){
        Page<EduTeacher> p1=new Page<>(current,limit);
        eduTeacherService.page(p1,null);
        long total = p1.getTotal();
        List<EduTeacher> records = p1.getRecords();

        return R.ok().data("total",total).data("items",records);
    }
*/

   @PostMapping("{current}/{limit}")
    public R selectTeachersByCondition(
           @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable long current,
           @ApiParam(name = "limit", value = "每页记录数", required = true)
           @PathVariable long limit,
           @RequestBody TeacherQuery teacherQuery){
        Page<EduTeacher> p2=new Page<>(current,limit);
        eduTeacherService.pageTeacherCondition(p2,teacherQuery);
       List<EduTeacher> items = p2.getRecords();
       long total = p2.getTotal();

       return R.ok().data("total",total).data("items",items);
    }
    @ApiOperation(value = "新增讲师")
    @PostMapping("saveTeacher")
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher){
        System.out.println(teacher);
        eduTeacherService.save(teacher);
        return R.ok();
    }
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("item", teacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("update/{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher){
        teacher.setId(id);
        eduTeacherService.updateById(teacher);
        return R.ok();
    }

}

